package com.creawor.km.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {
	
	
	static SimpleDateFormat sf = null;
	public static final String DEFAULT_KNOW_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String DEFAULT_KNOW1_FORMAT = "yyyy/MM/dd HH:mm:ss";
	public static String getStr(Date createdtime, String format) {
		if(null==createdtime)
			return null;
		if(null==format)
			format=DEFAULT_KNOW_FORMAT;
		sf =new SimpleDateFormat(format);
//		System.out.println("###################"+createdtime.getMonth());
		return sf.format(createdtime);
	}

	public static Date getDate(String createdtime, String format) {
		if(null==createdtime||createdtime.length()<1)
		{
			return null;
		}
		if(null==format)
			format=DEFAULT_KNOW_FORMAT;
		sf =new SimpleDateFormat(format);
		
		try {
			return sf.parse(createdtime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static Date parse(String datestr, String pattern) {
		Date date = null;
		
		if(null!=datestr && !"".equals(datestr.trim())){
			
			
			if(-1!=datestr.indexOf("-")){
				
				String ts[]=datestr.split("-");
				String year=ts[0];
				if(year.length()<3){
					datestr="20"+datestr;
					if(datestr.length()<5)
						datestr=datestr+"-15";
				}else{
					if(datestr.length()<8)
						datestr=datestr+"-15";
				}
				String md=ts[1];
				if(null!=md){
					if(md.length()<2){
						md="0"+md;
						datestr=datestr.substring(0,5)+md+datestr.substring(6,datestr.length());
					}
				}
				
			}
			if(-1!=datestr.indexOf("/")){
				
				String ts[]=datestr.split("/");
				String year=ts[0];
				if(year.length()<3){
					datestr="20"+datestr;
					if(datestr.length()<5)
						datestr=datestr+"/15";
				}else{
					if(datestr.length()<8)
						datestr=datestr+"/15";
				}
				String md=ts[1];
				if(null!=md){
					if(md.length()<2){
						md="0"+md;
						datestr=datestr.substring(0,5)+md+datestr.substring(6,datestr.length());
					}
				}
				
			}
		}
		
		if (null == pattern || "".equals(pattern)) {
			if(-1!=datestr.indexOf("-"))
				pattern = DEFAULT_KNOW_FORMAT;
			else
				pattern = DEFAULT_KNOW1_FORMAT;
		}

		try {
			if(datestr.length()<11){
				datestr+=" 00:00:00";
			}
			SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
			date = dateFormat.parse(datestr);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return date;
	}
	
	public static String getCurrentYear(){
		return java.util.Calendar.getInstance().get(Calendar.YEAR)+"";
	}
	
	public static String getCurrentMonth(){
		return java.util.Calendar.getInstance().get(Calendar.MONTH)+"";
	}
	
	public static void main(String[] args){
//		DateUtil.parse("2008/2/30", null);
		String datestr=DateUtil.getStr(new Date(), null);
		System.out.println("datestr="+datestr);
		
	}
}
